%面阵
close all;
clc
N=16;% y
M=16; % x
%阵元个数，M*N
f=26000000000;%信号频率`
c=300000000;
lambda=c/f;
dy=0.5*lambda;
dz=0.5*lambda;
theta = linspace(-180,180,1001);  %方位角
phi = linspace(-90,90,501);  %俯仰角 
%%%%%%%%%%%%%%
a=17;
b=17;
abcase=289;
alpha1= linspace(0,90,a); %扫描角
phi1=linspace(0,90,b);
CDbook = zeros(abcase,M*N);%初始化一个矩阵289*256
%%%%%%%%%%
phi0 = 0;   %俯仰角
theta0 = 0;%预先设定的方向，方位角

%%%%%
k = 1;
p = zeros(3,M*N);
%===============================================================================%
for iii = 0 : M-1+1
    for jjj = 0 : N-1+1
        p(:,k) = [0,jjj*dy,-iii*dz]';
        k = k+1;
    end
end   %阵元的位置

for n=1:17 %去除中间的第9列
    p(:,17*(n-1)+9-(n-1))=[];
    %gapCDbook = [gapCDbook(:,1:17*(n-1)+8-(n-1)) gapCDbook(:,17*(n-1)+10-(n-1):(290-n))];
end
p(:,16*8+1:16*8+16)=[];
%gapCDbook = [gapCDbook(:,1:16*8) gapCDbook(:,16*8+17:272)]; %去除中间的第9行
%==================================产生码字=======================================%

for Len1 = 1:N
    for Len2 = 1:M
        index = (Len1-1)*16+Len2;
        k2= 2*pi/lambda*[ sin(phi1(Len2)*pi/180)*cos(alpha1(Len1)*pi/180), sin(phi1(Len2)*pi/180)*sin(alpha1(Len1)*pi/180), cos(phi1(Len2)*pi/180)];
        w2= exp(1i*k2*p);
        CDbook(index,:) = w2;
    end
end

%================================2D-DFT===============================================%

for a1_Len = 1 : abcase
    for row1 = 1 : M*N
        if((angle(CDbook(a1_Len,row1)) > -80*pi/180) && (angle(CDbook(a1_Len,row1)) < 100*pi/180) )
            CDbook(a1_Len,row1) = 0; %200度相偏 -80~100 左1高电压 
        else
            CDbook(a1_Len,row1) = 1;
        end
    end
end

% %===================================映射=========================================%
mdcbook = zeros(1,M*N);
for row =1 : abcase
    for i = 1 : 8
        for j = 1 : 16
            mdcbook (row , 8*(j-1)+i) = CDbook(row, 16*(i-1)+j);
        end
    end
    for i = 1 : 8
        for j = 1 : 16
            mdcbook (row , 8*(j-1+16)+i) = CDbook(row, 16*(i-1+8)+j);
        end
    end
end

% xlswrite ('cdbookplus.xlsx',CDbook);
% %===================================1 bit 量化=========================================%
% for a1_Len = 1 : M*N
%     for row1 = 1 : M*N
%         if(real(CDbook(a1_Len,row1)) >= 0)
%             CDbook(a1_Len,row1) = 1;
%         else
%             CDbook(a1_Len,row1) = 0;
%         end
%     end
% end
% 
% 
% % for a1_Len = 1 : M*N
% %     for row1 = 1 : M*N
% %         if(real(CDbook(a1_Len,row1)) > 0)
% %             CDbook1(a1_Len,row1) = 1;
% %         else
% %             CDbook1(a1_Len,row1) = -1;
% %         end
% %     end
% % end
% 
% % dcdcbook = zeros(64 , 16);
% % dcdcbook = zeros(256 , 64);

% %===================================去除边界=========================================%
% gapCDbook = CDbook;
% for n=1:17 %去除中间的第9列
%     gapCDbook = [gapCDbook(:,1:17*(n-1)+8-(n-1)) gapCDbook(:,17*(n-1)+10-(n-1):(290-n))];
% end
% gapCDbook = [gapCDbook(:,1:16*8) gapCDbook(:,16*8+17:272)]; %去除中间的第9行
% %===================================合并为16进制=========================================%
dcdcbook = zeros(abcase,M*N/4);
for row = 1 : abcase
    for column = 1 : M*N/4
        dcdcbook (row , column) = mdcbook(row , 4 * column - 3 ) * 8 + mdcbook(row , 4 * column - 2 ) * 4 + mdcbook(row , 4 * column  - 1 ) * 2 + mdcbook(row , 4 * column  ) * 1;
    end
end

%写入xlsx  原始数据
%xlswrite ('CDbookplus.xlsx',dcdcbook);

% %===================================输出=========================================%
%合并RIS板的每一列
prCDbook = zeros(abcase,M*N/8);
for row = 1 : abcase
    for column = 1 : M*N/8
        prCDbook (row , column) = dcdcbook(row , 2 * column  - 1 ) * 16 + dcdcbook(row , 2 * column  ) * 1;
    end
end
dlmwrite('289256CDbook.csv',prCDbook,'delimiter',',');
% %===================================去重=========================================%
same = zeros(2, 100);%存储重复情况
id=0;
del=0;
for row = 1 : abcase
    for n=row+1:abcase
        sm=0;
        for column = 1 : M*N/8
            if(prCDbook (row , column)~=prCDbook (n, column))
                sm=1;
                break;
            end
        end
        if(sm==0)
            id=id+1;
            same(1,id) = row;
            same(2,id) = n;
        end
    end
end

for ID = 1:20  %20为与第1行重复数量
    prCDbook(same(2,ID)-ID+1,:)=[];%去除重复1
end

same2 = zeros(2, 10);%存储重复情况
id=0;
del=0;
for row = 1 : 289-20
    for n=row+1:289-20
        sm=0;
        for column = 1 : M*N/8
            if(prCDbook (row , column)~=prCDbook (n, column))
                sm=1;
                break;
            end
        end
        if(sm==0)
            id=id+1;
            same2(1,id) = row;
            same2(2,id) = n;
        end
    end
end

for ID = 1:32  %32为与第237行重复数量
    prCDbook(same2(2,ID)-ID+1,:)=[];%去除重复237
end

same3 = zeros(2, 1);%存储重复情况
id=0;
del=0;
for row = 1 : 289-20-32
    for n=row+1:289-20-32
        sm=0;
        for column = 1 : 32
            if(prCDbook (row , column)~=prCDbook (n, column))
                sm=1;
                break;
            end
        end
        if(sm==0)
            id=id+1;
            same3(1,id) = row;
            same3(2,id) = n;
        end
    end
end

%prCDbook(same3(2,1),:)=[];%去除重复220

% same4 = zeros(2, 1);%存储重复情况
% id=0;
% del=0;
% for row = 1 : 289-33-5-1
%     for n=row+1:289-33-5-1
%         sm=0;
%         for column = 1 : 32
%             if(prCDbook (row , column)~=prCDbook (n, column))
%                 sm=1;
%                 break;
%             end
%         end
%         if(sm==0)
%             id=id+1;
%             same4(1,id) = row;
%             same4(2,id) = n;
%         end
%     end
% end


prCDbook0=prCDbook(:,1:16);
prCDbook1=prCDbook(:,17:32);

dlmwrite('4x64CDbook.csv',prCDbook,'delimiter',',');
dlmwrite('4x64CDbook0.csv',prCDbook0,'delimiter',',');
dlmwrite('4x64CDbook1.csv',prCDbook1,'delimiter',',');


% % 
% % %===============================================================================%
% % %                    波束扫描  Forming beam pattern                             %
% % %===============================================================================%
% for ii=1:length(theta)
%     for jj=1:length(phi)
%         k = -2*pi/lambda*[cos(phi(jj)*pi/180)*cos(theta(ii)*pi/180)-cos(phi0*pi/180)*cos(theta0*pi/180),cos(phi(jj)*pi/180)*sin(theta(ii)*pi/180)-cos(phi0*pi/180)*sin(theta0*pi/180),sin(phi(jj)*pi/180)-sin(phi0*pi/180)];
%         v=exp(-1i*k*p);
%         b(ii,jj)=CDbook1(17,:)*v';
%     end
% end
% 
% B=20*log10(abs(b)/max(max(abs(b))));
% 
% %===============================================================================%
% figure(4);
% theta_radians = deg2rad(theta);
% polarplot(theta_radians,B(:,256),'r')
% rlim([-70 0])
% legend('俯仰角phi=0')
% text(4,0.5,'0')
% % str=['B(0,45)=' num2str( absB(626,251))];
% % text(pi/4,0,str);
% grid on,hold on